'use strict';
import { uuid } from './regular'

export default app => {
  app.validator.addRule('mongo-id', (_, value) => {
    if (!/^[a-zA-Z0-9]{24}$/.test(value)) {
      return '参数必须是MongoDB id';
    }
  });
  app.validator.addRule('uuid', (_, value) => {
    if (!uuid.test(value)) {
      return '参数必须是 UUID 类型';
    }
  });
  app.validator.addRule('w-date', (_, value) => {
    const dateStr = new Date(value).toISOString();
    if (dateStr !== value) {
      return '参数必须是Date类型';
    }
  });
  app.validator.addRule('w-date-num', (_, value) => {
    const date = new Date(typeof value === 'number' ? value : parseInt(value))
    if (isNaN(date.getTime())) {
      return '时间字段有误';
    }
  });
};
